package com.liangtian.mdm.system.user.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liangtian.mdm.base.exception.TransactionalException;
import com.liangtian.mdm.system.menu.model.Menu;
import com.liangtian.mdm.system.button.dao.SysButtonDao;
import com.liangtian.mdm.system.group.dao.SysGroupDao;
import com.liangtian.mdm.system.organization.model.SysOrganization;
import com.liangtian.mdm.system.organization.service.SysOrganizationService;
import com.liangtian.mdm.system.permission.model.SysRole;
import com.liangtian.mdm.system.ugroup.dao.SysUgroupDao;
import com.liangtian.mdm.system.ugroup.dao.SysUgroupUserDao;
import com.liangtian.mdm.system.ugroup.model.SysUgroup;
import com.liangtian.mdm.system.ugroup.model.SysUgroupUser;
import com.liangtian.mdm.system.user.dao.SysUserDao;
import com.liangtian.mdm.system.user.model.SysUser;
import com.liangtian.mdm.system.user.service.SysUserService;
import com.liangtian.mdm.util.ExcelUtil;
import com.liangtian.mdm.util.StringUtil;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Encoding    :  UTF-8</p>
 * <p>Package Name: com.liangtian.mdm.system.user.service.impl</p>
 * <p>Project Name: chccig-dm </p>
 * <p>Copyright   :  Copyright(c) 2016 石化盈科信息技术有限责任公司 </p>
 * <p>Created Time: 2016-04-28 09:17</p>
 * <p>Description : 用户实现服务接口</p>
 *
 * @author : Administrator
 * @version :  1.0
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    //注入用户dao层接口
    @Autowired
    SysUserDao sysUserDao;

    @Autowired
    SysUgroupDao sysUgroupDao;

    @Autowired
    SysUgroupUserDao sysUgroupUserDao;

    @Autowired
    SysButtonDao sysButtonDao;

    @Autowired
    SysGroupDao sysGroupDao;

    @Autowired
    SysOrganizationService sysOrganizationServiceImpl;

    ////@Value("${ConstantBean.CURRENT_SESSION_USER}")
    //private String ConstantBean.CURRENT_SESSION_USER;

    /**
     * @param user
     * @return 分页条件下的
     * @desc 获取未提交用户列表
     * @params参数详情 pageNum 总页数
     * @params参数详情 length 每页显示条数
     * @params参数详情 start 起始条数
     * @params参数详情 draw 请求次数
     */
    @Override
    public PageInfo selectUserListWithNotSubmit(Map<String, Object> map) {
        PageInfo<Map<String, Object>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            String userStatus = (String) map.get("userStatus");
            if (!StringUtil.isBlank(userStatus)) {
                userStatus = "'" + userStatus.replaceAll(",", "','") + "'";
                map.put("userStatus", userStatus);
            }

            List<Map<String, Object>> userMapList = sysUserDao.selectUserListWithNotSubmit(map);
            pageInfo = new PageInfo<Map<String, Object>>(userMapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /**
     * @param user
     * @return 分页条件下的
     * @desc 获取全部用户信息(包含查询)
     * @params参数详情 pageNum 总页数
     * @params参数详情 length 每页显示条数
     * @params参数详情 start 起始条数
     * @params参数详情 draw 请求次数
     */
    @Override
    public PageInfo selectUserListByCondition(Map<String, Object> map) {
        PageInfo<Map<String, Object>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）
            if (map.get("status").toString() != null || map.get("status").toString() != "") {
                String[] userStatus = map.get("status").toString().split(",");
                map.put("status", userStatus);
            }
            List<Map<String, Object>> userMapList = sysUserDao.selectUserListByCondition(map);
            pageInfo = new PageInfo<Map<String, Object>>(userMapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /**
     * @param user
     * @return 分页条件下的
     * @desc 获取全部用户信息(包含查询)
     * @params参数详情 pageNum 总页数
     * @params参数详情 length 每页显示条数
     * @params参数详情 start 起始条数
     * @params参数详情 draw 请求次数
     */
    @Override
    public PageInfo selectUserPageByCondition(Map<String, Object> map) {

        PageInfo<List<SysUser>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            List<SysUser> userList = sysUserDao.selectUserOrginazation(map);
            pageInfo = new PageInfo(userList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /**
     * @param user 对象
     * @return 受影响的行数
     * @des 新增user
     */
    @Override
    public int insertUser(SysUser user) {
        //新增用户
        int count = sysUserDao.insertUser(user);
        Map<String, Object> insertParams = new HashMap<>();
        insertParams.put("roleId", 1L);
        insertParams.put("userId", user.getUserId());
        //插入中间表数据
        sysUserDao.insertUserRoleRef(insertParams);
        return count;
    }

    /**
     * @param user 对象
     * @return 受影响的行数
     * @desc 编辑 （编辑数据；启用-1；禁用-0）
     */
    @Override
    public int updateUser(SysUser user, Long[] ids) throws TransactionalException {
        int result = 0;
        int num = 0;
        //编辑用户
        int count = sysUserDao.updateUser(user);
        if (count > 0) {
            try {
                //删除用户的角色的关联表
                Map<String, Object> deleteParams = new HashMap<>();
                deleteParams.put("userId", user.getUserId());
                //删除角色和用户中间表数据
                sysUserDao.deleteUserRoleRef(deleteParams);
                //插入中间表数据
                Map<String, Object> insertParams = new HashMap<>();
                if (ids.length != 0) {
                    insertParams.put("ids", ids);
                    insertParams.put("userId", user.getUserId());
                    num = sysUserDao.insertUserRoleRef(insertParams);
                }
                if (num == ids.length) {
                    result = 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            throw new TransactionalException("编辑用户事务异常");
        }
        return result;
    }

    /**
     * @param user 对象
     * @return 受影响的行数
     * @desc 编辑 （编辑数据；启用-1；禁用-0）
     */
    @Override
    public int updateUser(SysUser user) {
        int result = 0;
        //编辑用户
        int count = sysUserDao.updateUser(user);
        result = count;
        return result;
    }

    /**
     * @param params
     * @return 受影响的行数
     * @desc 批量删除用户信息
     * @params参数详情 idsList 用户id
     */
    @Override
    public int batchDeleteUser(Map<String, Object> params) throws TransactionalException {
        int result = 0;
        //删除中间表
        int num = sysUserDao.deleteUserRoleRefMap(params);
        if (num > 0) {
            //删除用户
            try {
                int count = sysUserDao.batchDeleteUser(params);
                if (count > 0) {
                    result = 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            throw new TransactionalException("删除用户事务异常");
        }
        return result;
    }

    /**
     * @param params
     * @return 受影响的行数
     * @desc 校验用户账户
     * @params参数详情 userId 用户id
     * @params参数详情 userAccount 用户账号
     */
    @Override
    public int selectUserAccount(Map<String, Object> params) {
        return sysUserDao.selectUserAccount(params);
    }

    /**
     * @param params
     * @return 受影响的行数
     * @desc 编辑用户状态
     * @params参数详情 idsList 用户id
     * @params参数详情 userStatus 用户状态
     */
    @Override
    public int editUserStatus(Map<String, Object> params) {
        return sysUserDao.editUserStatus(params);
    }

    /**
     * 根据id查询用户
     *
     * @param user
     * @return
     */
    @Override
    public SysUser selectUserById(SysUser user) {
        return sysUserDao.selectUserById(user);
    }

    /**
     * 根据id批量查询用户
     *
     * @param user
     * @return
     */
    @Override
    public List<SysUser> selectUserByIds(String ids) {
        return sysUserDao.selectUserByIds(ids);
    }

    /**
     * 根据id查询用户
     *
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> selectUserWithOrganizaNameById(SysUser user) {
        return sysUserDao.selectUserWithOrganizaNameById(user);
    }

    /**
     * 查询角色信息
     *
     * @param user
     * @return
     */


    @Override
    public List<SysRole> selectRole(SysUser user) {
        return sysUserDao.selectRole(user);

    }

    /**
     * @param params 用户帐号
     * @return 用户信息
     * @desc 根据帐号查询用户
     */
    @Override
    public SysUser queryUserByUserAccount(String userAccount) {
        return sysUserDao.queryUserByUserAccount(userAccount);
    }

    /**
     * 查询用户组信息
     *
     * @param userId
     * @return
     */
    @Override
    public List<Map<String, Object>> selectUserGroup(Long userId) {
        return sysUserDao.selectUserGroup(userId);
    }

    /**
     * 查询菜单树
     *
     * @param userId
     * @return
     */
    @Override
    public List<Menu> menuTree(Map<String, Object> params) {
        return sysUserDao.queryMenuTree(params);
    }

    /**
     * @param Map
     * @return 分页条件下的用户组
     * @desc 获取用户组信息
     * @params参数详情 pageNum 总页数
     */
    @Override
    public PageInfo queryUserGroup(Map<String, Object> map, SysUser user) {
        PageInfo<Map<String, Object>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            List<Map<String, Object>> userMapList = sysUserDao.selectUserGroup(user.getUserId());
//			List<Map<String,Object>> userMapList = sysUserDao.selectUserGroup(user);
            pageInfo = new PageInfo<Map<String, Object>>(userMapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Override
    public PageInfo queryUserGroupList(Map<String, Object> map) {

        PageInfo<List<SysUgroup>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            List<SysUgroup> userGroupList = sysUgroupDao.selectUserGroupList(map);
            pageInfo = new PageInfo(userGroupList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @SuppressWarnings({"rawtypes"})
    @Override
    public PageInfo queryTheUserGroup(Map<String, Object> param) {

        PageInfo<Map<String, Object>> pageInfo = null;
        try {
            Object pNo = param.get("pageNum");
            int pageNum, pageSize;
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(param.get("limit").toString());// 模型定义页面要显示数据条数
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            List<Map<String, Object>> userGroupList = sysUserDao.selectTheUserGroup(param);
            pageInfo = new PageInfo<Map<String, Object>>(userGroupList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /**
     * @param Map
     * @des 新增用户组
     */
    @Override
    public boolean insertUserGroup(Map<String, Object> param) {
        boolean flag = false;
        String[] groupIds = (String[]) param.get("groupIds");
        String userId = (String) param.get("userId");
        List<SysUgroupUser> userList = new ArrayList<SysUgroupUser>();
        for (int i = 0; i < groupIds.length; i++) {
            SysUgroupUser sysUgroupUser = new SysUgroupUser();
            sysUgroupUser.setUserId(userId);
            String groupId = groupIds[i];
            sysUgroupUser.setGroupId(groupId);
            userList.add(sysUgroupUser);
        }
        try {
            sysUgroupUserDao.insertUgroupUserBatch(userList);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * @param Map
     * @des 删除用户组
     */
    @Override
    public boolean deleteUserGroup(Map<String, Object> params) {
        boolean flag = false;
        try {
            sysUgroupUserDao.deleteUserGroup(params);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * @param Map
     * @des 用户审核
     */
    @Override
    public int approve(List<SysUser> list) {
        return sysUserDao.updateUserStatusBatch(list);
    }

    /**
     * 用户批量创建_删除
     */
    @Override
    public int delete(Map<String, Object> map) {
        return sysUserDao.batchDeleteUser(map);
    }

    /**
     * 用户密码重置
     */
    @Override
    public int resetPass(List<SysUser> sulst) {

        return sysUserDao.resetPass(sulst);

    }

    /**
     * 用户批量创建_提交
     */
    @Override
    public int submit(Map<String, Object> param) {

        List<SysUser> userList = new ArrayList<SysUser>();
        //修改数据ID
        String ids = param.get("ids").toString();

        String[] pks = ids.split(",");
        for (String pk : pks) {
            SysUser user = new SysUser();
            user.setUserId(Long.parseLong(pk));
            // 用户状态：待激活用户
//			user.setUserStatus("W");
            //如果是用户提交，审核通过，那么用户状态直接更改为批量已提交 -D
            user.setUserStatus("D");
            userList.add(user);
        }
        return sysUserDao.updateUserStatusBatch(userList);
    }

    /**
     * @desc 导入模板导出
     */
    @Override
    public void templeExport(HttpServletResponse resp) {
        try {
            OutputStream out = null;
            List<String[]> strLst = new ArrayList<String[]>();
            String[] account = {"用户账号", "必填\n不能重复"};
            strLst.add(account);
            String[] username = {"用户名称", "必填"};
            strLst.add(username);
            String[] userpass = {"用户密码", "必填(不能填写中文和横杠,只能输入由数字、26个英文字母或者下划线)"};//后期添加用户导出模板-（加了用户密码这项）
            strLst.add(userpass);
            String[] email = {"用户邮箱", "必填\n(校验规则 ：xxx@xx.cn/cm/com..)"};
            strLst.add(email);
            String[] tel = {"用户电话", "不必填\n(校验：0xx-xxxxxxxx或者0xxx-xxxxxxx（x）或者xxxxxxx(x))"};
            strLst.add(tel);
            String[] phone = {"用户手机", "必填\n长度为11位"};
            strLst.add(phone);
            String[] orgId = {"所属单位名称", "必填\n在第二个sheet中查找(组织机构名称)"};
            strLst.add(orgId);
            XSSFWorkbook workbook = new XSSFWorkbook(); // 声明一个工作薄，开始将上一类别的数据写入到excel
            CreationHelper factory = workbook.getCreationHelper();

            XSSFSheet sheet = workbook.createSheet("用户"); //声明一个工作薄的sheet页
            sheet.setDefaultColumnWidth(100);
            sheet.setDefaultRowHeight((short) (2*256));
            sheet.setDefaultColumnWidth(100);
            XSSFRow row = sheet.createRow(0); //创建这个sheet页的第一行,列名
            XSSFRow row1 = sheet.createRow(1);//创建第二行,收集数据的格式
            XSSFCellStyle normalStyle = workbook.createCellStyle();

            XSSFCellStyle redStyle = workbook.createCellStyle();
            XSSFFont font = workbook.createFont();
            font.setBold(true);
            font.setColor(HSSFColor.RED.index);
            redStyle.setFont(font);

            for (int i = 0; i < strLst.size(); i++) {
                if (!strLst.get(i)[0].equals("用户电话")) {
                    if (strLst.get(i)[0].equals("用户账号")) {
                        ExcelUtil.addCell(sheet, row, factory, i, redStyle, null, strLst.get(i));
                        ExcelUtil.addCell(sheet, row1, "必填、不能重复", i, normalStyle, null);
                    } else if (strLst.get(i)[0].equals("用户邮箱")) {
                        ExcelUtil.addCell(sheet, row, factory, i, redStyle, null, strLst.get(i));
                        ExcelUtil.addCell(sheet, row1, "邮箱格式:xxxxx@xx.cn/cm/com..", i, normalStyle, null);
                    } else if (strLst.get(i)[0].equals("用户手机")) {
                        ExcelUtil.addCell(sheet, row, factory, i, redStyle, null, strLst.get(i));
                        ExcelUtil.addCell(sheet, row1, "手机格式:1xxxxxxxxx", i, normalStyle, null);
                    } else {
                        ExcelUtil.addCell(sheet, row, factory, i, redStyle, null, strLst.get(i));
                    }
                } else {
                    ExcelUtil.addCell(sheet, row, strLst.get(i)[0], i, normalStyle, null);
                    ExcelUtil.addCell(sheet, row1, "电话格式: 0xx-xxxxxxxx 或者 0xxx-xxxxxxx(x)或者 xxxxxxx(x)", i, normalStyle, null);
                }
                sheet.setColumnWidth(i, strLst.get(i)[0].getBytes().length*256*2);
            }
            XSSFSheet mainSheet = workbook.createSheet("组织机构名录");//创建主表的sheet
            XSSFRow mainRow = mainSheet.createRow(0);
            List<String> orgLst = new ArrayList<String>();
            orgLst.add("组织机构ID");
            orgLst.add("组织机构名称");
            mainSheet.setDefaultColumnWidth(100);
            mainSheet.setDefaultRowHeight((short) (2*256));
            for (int i = 0; i < orgLst.size(); i++) {
                ExcelUtil.addCell(mainSheet, mainRow, orgLst.get(i), i, normalStyle, null);
            }

            int c1Len = "组织机构ID".getBytes().length, c2Len = "组织机构名称".getBytes().length;
            List<Map<String, Object>> list = sysOrganizationServiceImpl.queryOrganizationList(null);
            for (int i = 0; i < list.size(); i++) {
                XSSFRow ContentRow = mainSheet.createRow(i + 1);
                SysOrganization sysOrg = (SysOrganization) list.get(i);
                String value = Long.toString(sysOrg.getOrganizationId());
                String name = sysOrg.getOrganizationName();
                Cell cell = ContentRow.createCell(0);
                Cell cellTwo = ContentRow.createCell(1);
                cell.setCellValue(value);
                c1Len = value.getBytes().length > c1Len ? value.getBytes().length : c1Len;
                cellTwo.setCellValue(name);
                c2Len = name.getBytes().length > c2Len ? name.getBytes().length : c2Len;
            }
            mainSheet.setColumnWidth(0, c1Len*256);
            mainSheet.setColumnWidth(1, c2Len*256);
            workbook.setSheetName(0, "用户导入模板");
            out = resp.getOutputStream();
            ExcelUtil.setResponseHeader(resp, "用户导入模板");
            workbook.write(out);
            workbook.close();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * <p> 菜单权限复制</p>
     *
     * @param :      newUserId要赋给用户B的ID
     * @param userId 用户A的ID
     */
    @Override
    @Transactional
    public int updateUserMenuAuthority(Map<String, Object> param) {
        int result = 0;
        // 权限复制给多用户，获取用户的用户ID
        String ids = param.get("ids").toString();
        String[] pks = ids.split(",");
        // 逐个复制用户菜单权限
        for (String pk : pks) {
            Map<String, Object> params = new HashMap<>();
            params.put("newUserId", pk);
            params.put("userId", param.get("userId"));

            result = sysUserDao.insertUserRole(params);
        }

        return result;
    }

    /*** <p>Title:</p>
     * <p>Description: </p>
     * @param : @param userAccount
     * @param : @return
     * @author : xiaoliu
     * @date :2016年10月27日下午2:58:42
     */

    @Override
    public int queryExistByAccount(String userAccount) {

        return sysUserDao.queryUserByAccount(userAccount);
    }

    /*** <p>Title:</p>
     * <p>Description: </p>
     * @param : @param map
     * @param : @return
     * @author : xiaoliu
     * @date :2016年11月2日下午1:25:30
     */

    @Override
    public PageInfo selectShUserListByCondition(Map<String, Object> map) {

        PageInfo<Map<String, Object>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）

            List<Map<String, Object>> userMapList = sysUserDao.selectUserListByCondition(map);
            pageInfo = new PageInfo<Map<String, Object>>(userMapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /*** <p>Title:</p>
     * <p>Description: </p>
     * @param : @param map
     * @param : @return
     * @author : xiaoliu
     * @date :2016年11月2日下午3:17:11
     */

    @Override
    public int updateByIds(Map<String, Object> map) {

        return sysUserDao.updateByIds(map);

    }

    /**
     * * <p>Title: 查询不是本用户下，所有活动用户</p>
     * <p>Description:  </p>
     * <p>Company:@param map
     * <p>Company:@return </p>
     *
     * @author : xiaoliu
     * @date :2016年11月3日下午4:07:19
     */

    @Override
    public PageInfo queryUserListWithoutUserId(Map<String, Object> map) {

        PageInfo<Map<String, Object>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）
            if (map.get("status").toString() != null || map.get("status").toString() != "") {
                String[] userStatus = map.get("status").toString().split(",");
                map.put("status", userStatus);
            }
            List<Map<String, Object>> userMapList = sysUserDao.queryUserListWithoutUserId(map);
            pageInfo = new PageInfo<Map<String, Object>>(userMapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /*** <p>Title: 按钮权限复制方法</p>*/
    @Override
    public int updateUserButtonAuthority(Map<String, Object> param) {
        int result = 0;
        // 权限复制给多用户，获取用户的用户ID
        String userId = param.get("userId").toString();
        String ids = param.get("ids").toString();
        String[] pks = ids.split(",");
        // 逐个复制用户按钮权限
        for (String pk : pks) {
            Map<String, Object> params = new HashMap<>();
            params.put("newUserId", pk);
            params.put("userId", userId);

            result = sysUserDao.insertUserPreButton(params);
        }
        return result;
    }


    /*** <p>Title: 按钮权限复制方法</p>*/
    @Override
    public int updateUserGroupAuthority(Map<String, Object> param) {
        int result = 0;
        String userId = (String) param.get("userId");
        // 权限复制给多用户，获取用户的用户ID
        String ids = param.get("ids").toString();
        String[] pks = ids.split(",");
        // 逐个复制用户按钮权限
        for (String pk : pks) {
            Map<String, Object> params = new HashMap<>();
            params.put("newUserId", pk);
            params.put("userId", userId);
            result = sysUserDao.insertUserPreGroup(params);
            sysUserDao.insertUserPreAttr(params);
        }
        return result;
    }

    /**
     * <p> 查询用户的</p>
     *
     * @author : xiaoliu
     * @date :2017年2月9日下午12:39:19
     */
    @Override
    public PageInfo queryUsers(Map<String, Object> map) {
        PageInfo<Map<String, Object>> pageInfo = null;
        try {
            Object pNo = map.get("pageNum");
            int pageNum, pageSize;
            // export
            if (pNo != null && !"NaN".equals(pNo)) {
                pageNum = Integer.parseInt(pNo.toString());// 页码
            } else {
                pageNum = 1;
            }
            // pageSize=0时不分页
            pageSize = Integer.parseInt(map.get("limit").toString());// 模型定义页面要显示数据条数
            // 例如
            // 10,20,50,100
            PageHelper.startPage(pageNum, pageSize);// 开始分页标志（当前页，显示条数）
            List<Map<String, Object>> userMapList = sysUserDao.queryUsers(map);
            pageInfo = new PageInfo<Map<String, Object>>(userMapList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    /**
     * <p>TODO</p>
     *
     * @param : @param user
     * @param : @return
     * @author : xiaoliu
     * @date :2017年3月20日下午3:43:29
     */

    @Override
    public int addUser(SysUser user) {
        return sysUserDao.insertUser(user);
    }

    @Override
    public SysUser mobileCheckLogin(String userAccount, String passWord) {
        //String user_salt = "dm";
        //passWord = CipherUtil.createPwdEncrypt(userAccount.trim(), passWord.trim(), user_salt.trim());
        Map<String, Object> param = new HashMap<>();
        param.put("userAccount", userAccount);
        param.put("passWord", passWord);
        return sysUserDao.mobileCheckLogin(param);
    }

    /**
     * <p>TODO 修改密码</p>
     *
     * @param : @param user
     * @param : @return
     * @author : xiaoliu
     * @date :2017年6月12日下午3:51:08
     */

    @Override
    public int updatePwd(Map map) {

        return sysUserDao.updatePwd(map);
    }

}
